// PrizePicker.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "PrizePicker.h"

#include <wincrypt.h>

// Generate a random seed value of cryptographic strength
static int GetRandomNumber()
{
	DWORD dwRandomSeed = rand();
	HCRYPTPROV hCrypt;
	
	if (CryptAcquireContext(&hCrypt, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
	{
		CryptGenRandom(hCrypt, sizeof(dwRandomSeed), (BYTE*)&dwRandomSeed);

		CryptReleaseContext(hCrypt, 0);
	}

	return dwRandomSeed;
}

INT_PTR CALLBACK PrizePickerDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
				SHMENUBARINFO mbi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);
				
				// Create a menu bar
				memset(&mbi, 0, sizeof(SHMENUBARINFO));
				mbi.cbSize     = sizeof(SHMENUBARINFO);
				mbi.hwndParent = hDlg;
				mbi.nToolBarId = IDR_MENU;
				mbi.hInstRes   = GetModuleHandle(NULL);
				mbi.dwFlags = SHCMBF_HMENU;
				SHCreateMenuBar(&mbi);

				// Seed the random number generator.
				srand(GetRandomNumber());
            }
            return (INT_PTR)TRUE;

        case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_GENERATE:
					{
						int result = IDYES;

						if (SendDlgItemMessage(hDlg, IDC_LIST_WINNERS, LB_GETCOUNT, 0, 0) > 0)
						{
							result = MessageBox(hDlg, L"Are you sure? This action will clear Winners list.",
								L"Alert", MB_YESNO | MB_ICONASTERISK);
						}

						if (result == IDYES)
						{
							int start = GetDlgItemInt(hDlg, IDC_EDIT_START, NULL, FALSE);
							int end = GetDlgItemInt(hDlg, IDC_EDIT_END, NULL, FALSE);

							if (end < start)
							{
								MessageBox(hDlg, L"End Value must be greater that Start Value.", L"Fail", MB_OK | MB_ICONASTERISK);
							}
							else
							{
								// Clear out hopefuls and winners lists.
								SendDlgItemMessage(hDlg, IDC_LIST_HOPEFULS, LB_RESETCONTENT, 0, 0);
								SendDlgItemMessage(hDlg, IDC_LIST_WINNERS, LB_RESETCONTENT, 0, 0);

								// Populate the hopefuls list
								for (int i = start; i <= end; i++)
								{
									WCHAR szBuffer[16];
									swprintf(szBuffer, L"%lu", i);
									SendDlgItemMessage(hDlg, IDC_LIST_HOPEFULS, LB_ADDSTRING, 0, (LPARAM)szBuffer);
								}

								// Enable the "draw" menu item
								HWND hWndMB = SHFindMenuBar(hDlg);
								TBBUTTONINFO tbbi;
								memset(&tbbi,0,sizeof(tbbi));
								tbbi.cbSize = sizeof(tbbi);
								tbbi.dwMask = TBIF_STATE;
								tbbi.fsState = TBSTATE_ENABLED;
								SendMessage(hWndMB,TB_SETBUTTONINFO,IDC_DRAW,(LPARAM)&tbbi);
								DrawMenuBar(hWndMB);
							}
						}
					}
					break;

				case IDC_DRAW:
					if (SendDlgItemMessage(hDlg, IDC_LIST_HOPEFULS, LB_GETCOUNT, 0, 0) == 0)
					{
						MessageBox(hDlg, L"Everybody has won something go home.", L"Alert", MB_OK | MB_ICONASTERISK);
					}
					else
					{
						// Randomly select a person from the hopeful list
						int seed = rand();
						int pick = seed % SendDlgItemMessage(hDlg, IDC_LIST_HOPEFULS, LB_GETCOUNT, 0, 0);

						// Remove the person from the hopeful list and add
						// them to the winners list
						WCHAR szWinner[16];
						SendDlgItemMessage(hDlg, IDC_LIST_HOPEFULS, LB_GETTEXT, pick, (LPARAM)szWinner);
						SendDlgItemMessage(hDlg, IDC_LIST_HOPEFULS, LB_DELETESTRING, pick, 0);
						SendDlgItemMessage(hDlg, IDC_LIST_WINNERS, LB_ADDSTRING, 0, (LPARAM)szWinner);
					}
					break;

				case IDOK:
					EndDialog(hDlg, LOWORD(wParam));
					return (INT_PTR)TRUE;
			}
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return (INT_PTR)TRUE;

    }
    return (INT_PTR)FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	return DialogBox(GetModuleHandle(NULL), (LPCTSTR)IDD_PRIZEPICKER, NULL, PrizePickerDialog);
}